package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.domain.WechatMpInfo;
import org.locker.domain.bo.WechatMpInfoBo;
import org.locker.domain.vo.WechatMpInfoVo;
import org.locker.mapper.WechatMpInfoMapper;
import org.locker.service.IWechatMpInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统参数Service业务层处理
 *
 * @author winnie
 * @date 2024-05-21
 */
@RequiredArgsConstructor
@Service
public class WechatMpInfoServiceImpl  extends ServiceImpl<WechatMpInfoMapper, WechatMpInfo>  implements IWechatMpInfoService {

    private final WechatMpInfoMapper baseMapper;

    /**
     * 查询系统参数
     */
    @Override
    public WechatMpInfoVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询系统参数列表
     */
    @Override
    public TableDataInfo<WechatMpInfoVo> queryPageList(WechatMpInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WechatMpInfo> lqw = buildQueryWrapper(bo);
        Page<WechatMpInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统参数列表
     */
    @Override
    public List<WechatMpInfoVo> queryList(WechatMpInfoBo bo) {
        LambdaQueryWrapper<WechatMpInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WechatMpInfo> buildQueryWrapper(WechatMpInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WechatMpInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), WechatMpInfo::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAppId()), WechatMpInfo::getAppId, bo.getAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getAppSecret()), WechatMpInfo::getAppSecret, bo.getAppSecret());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), WechatMpInfo::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getToken()), WechatMpInfo::getToken, bo.getToken());
        lqw.eq(StringUtils.isNotBlank(bo.getEncodingAesKey()), WechatMpInfo::getEncodingAesKey, bo.getEncodingAesKey());
        lqw.eq(bo.getStatus() != null, WechatMpInfo::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增系统参数
     */
    @Override
    public Boolean insertByBo(WechatMpInfoBo bo) {
        WechatMpInfo add = MapstructUtils.convert(bo, WechatMpInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改系统参数
     */
    @Override
    public Boolean updateByBo(WechatMpInfoBo bo) {
        WechatMpInfo update = MapstructUtils.convert(bo, WechatMpInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WechatMpInfo entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统参数
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
